var express = require('express');
var router = express.Router();

const pool = require('../pool.js')

router.get("/setscore", (req, res) => {
  var sql = "insert into demo_score (gametyp , userid , date , score) values (? , ? , ? , ?) ;"
  pool.query(sql, [req.query.gametyp, req.query.userid, req.query.date, req.query.score], (err, result) => {
    if (err) throw err
    if (result.affectedRows == 1) {
      res.send({
        code: "1",
        msg: "分数上传成功"
      })
      console.log(`id为 ${req.query.userid} 的用户成功上传了一条分数数据 , 时间:  ${new Date().toLocaleString()}`)
    } else {
      res.send({
        code: "0",
        msg: "分数上传失败,请联系开发人员"
      })

    }
  })
})

router.get("/getscore", (req, res) => {
  var sql = "select * from demo_score where userid = ? ;"
  pool.query(sql, [req.query.userid], (err, result) => {
    if (err) throw err
    res.send(result)
    console.log(`id为 ${req.query.userid} 的用户成功查询了分数数据 , 时间:  ${new Date().toLocaleString()}`)
  })
})

router.get("/searchscore", (req, res) => {
  var sql = "select * from demo_score where userid = ? and gametyp = ? ;"
  pool.query(sql, [req.query.userid, req.query.gametyp], (err, result) => {
    if (err) throw err
    res.send(result)
    console.log(`id为 ${req.query.userid} 的用户成功查询了 ${req.query.gametyp} 分数数据 , 时间:  ${new Date().toLocaleString()}`)
  })
})

module.exports = router;